﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginInterface;
using Ch.Admin.Bfs.Sms.Sdmx.Registry.Proxies;
using Ch.Admin.Bfs.Sms.Sdmx.Editor.Utils;
using Ch.Admin.Bfs.Sms.Sdmx.Registry.Rights;
using Ch.Admin.Bfs.Sms.Sdmx.Registry;

using SdmxMl.Multiplexor;

namespace SmsUsers
{
    public partial class UcSmsAdmin : UserControl, IPlugin
    {
        private const int TAB_INDEX_USER_ROLE = 0;
        private const int TAB_INDEX_RIGHTS = 1;

        private HashSet<Language> languages;

        private WcfAdministrationProxy administrationProxy;
        private ISecuredArtefactRegistry artefactRegistry;

        public UcSmsAdmin()
        {
            InitializeComponent();

            languages = new HashSet<Language>();
            languages.Add(new Language("fr", "Français", "French"));
            languages.Add(new Language("de", "Deutsch", "German"));
            languages.Add(new Language("en", "English", "English"));
            languages.Add(new Language("it", "Italiano", "Italian"));


        }

        #region IPlugin Members

        /// <summary>Event is part of interface but not used </summary>
        public event PluginEvent PluginEventExposed
        {
            add { }
            remove { }
        }

        public void Init(System.Xml.XmlDocument xDoc, object obExtend)
        {

        }

        public UserControl VisualComponent()
        {
            return this;
        }

        public bool RequiresInit
        {
            get { return false; }
        }

        #endregion

        /// <summary> On plug-in load event </summary>
        /// <param name="sender"></param><param name="e"></param>
        private void UcSmsAdmin_Load(object sender, EventArgs e)
        {
            try
            {
                this.saveToolStripMenuItem.Enabled = false;
                if (Mux.IsNewSmsRegistry)
                {
                    administrationProxy = Mux.Instance.GetAdminRegistry();
                    artefactRegistry = Mux.Instance.GetNewRegistry();
                    this.saveToolStripMenuItem.Enabled = Mux.Instance.UserIsAdmin;
                    this.ucRoleEditor.LoadRoles(administrationProxy.GetRoleUsers());

                    this.ucArtefactRightEditor.SetLanguages(languages);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void tabControl_Selecting(object sender, TabControlCancelEventArgs e)
        {
            // Do we ask for saving ?, needs a dirty flag

        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (administrationProxy != null)
                {
                    using (new SdmxMl.Helper.CWaitCursor())
                    {
                        switch (tabControl.SelectedIndex)
                        {
                            case TAB_INDEX_USER_ROLE:
                                administrationProxy.UpdateRoleUsers(this.ucRoleEditor.GetRoles());
                                break;
                            case TAB_INDEX_RIGHTS:
                                Dictionary<string, Dictionary<IArtefactFilter, HashSet<OperationType>>> rights = artefactRegistry.GetPermissions();
                                this.ucArtefactRightEditor.LoadRights(rights);
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (administrationProxy != null)
                {
                    using (new SdmxMl.Helper.CWaitCursor())
                    {
                        switch (tabControl.TabIndex)
                        {
                            case TAB_INDEX_USER_ROLE:
                                administrationProxy.UpdateRoleUsers(this.ucRoleEditor.GetRoles());
                                break;
                        }

                        MessageBox.Show("Your modifications are saved.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (administrationProxy != null)
                {
                    using (new SdmxMl.Helper.CWaitCursor())
                    {
                        switch (tabControl.TabIndex)
                        {
                            case TAB_INDEX_USER_ROLE:
                                this.ucRoleEditor.LoadRoles(administrationProxy.GetRoleUsers());
                                break;
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


    }
}
